<?php
namespace Admin\Controller;

class ServiceContentController extends BaseController
{
    public function indexAction()
    {
        $Service = D('Activity');
        $service_id = I("get.service_id", 0, 'intval');
		$service_info = $Service->find($service_id);
        if (false === $service_info) {
            $this->error('无效的参数！');
        }
        $this->assign('service_id', $service_id);
		$this->assign('service_info', $service_info);
        
        $ServiceContent = D('ServiceContent');
        $condition = array('service_id' => $service_id);
		$count = $ServiceContent->where($condition)->count();
		$Page = new \Think\Page($count, 50);
		$show = $Page->show();
		$list = $ServiceContent->where($condition)->order('sort asc, id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		$this->assign('page', $show);
		$this->assign('list', $list);
        $this->display();
    }
    
    public function addAction()
    {
        $service_id = I("get.service_id", 0, 'intval');
        if (IS_POST) {
            $ServiceContent = D('ServiceContent');

            //模板图片文字处理
            $arr_pic = $_POST['TravelContent']['pic'];
            if (is_array($arr_pic) && !empty($arr_pic)) {
                $_POST['ServiceContent']['pic'] = implode('|', $arr_pic);
            }
            $arr_content = $_POST['TravelContent']['content'];
            if (is_array($arr_content) && !empty($arr_pic)) {
                $_POST['ServiceContent']['content'] = serialize($arr_content);
            }
            
            $data = $ServiceContent->create($_POST['ServiceContent']);
            if (false !== $data) {
                $insertId = $ServiceContent->add();
                if (false !== $insertId) {
                    $this->success('新增成功');
                } else {
                    $this->error('新增失败（失败原因：' . $ServiceContent->getError() . '）');
                }
            } else {
                $this->error($ServiceContent->getError());
            }
        } else {
            $this->assign('service_id', $service_id);
            $this->display();  
        }
    }
    
    public function updateAction()
    {
        $service_id = I("get.service_id", 0, 'intval');
        $ServiceContent = D('ServiceContent');
        if (IS_POST) {
            //模板图片文字处理
            $arr_pic = $_POST['TravelContent']['pic'];
            if (is_array($arr_pic) && !empty($arr_pic)) {
                $_POST['ServiceContent']['pic'] = implode('|', $arr_pic);
            }
            $arr_content = $_POST['TravelContent']['content'];
            if (is_array($arr_content) && !empty($arr_pic)) {
                $_POST['ServiceContent']['content'] = serialize($arr_content);
            }
            $data = $ServiceContent->create($_POST['ServiceContent']);
            if (false !== $data) {
                if (false !== $ServiceContent->save()) {
                    $this->success('更新成功', U('ServiceContent/index', array('service_id' => $service_id)));
                } else {
                    $this->error('更新失败（失败原因：' . $ServiceContent->getError() . '）');
                }
            } else {
                $this->error($ServiceContent->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $service_content_info = $ServiceContent->find($id);
            if (false === $service_content_info) {
                $this->error('获取信息失败！');
            }

            //模板图片处理
            $arr_pic = explode('|', $service_content_info['pic']);
            $arr_content = unserialize($service_content_info['content']);
            $this->assign('arr_content', $arr_content);
            $this->assign('arr_pic', $arr_pic);
            $this->assign('tpl_content', $this->_getTpl($service_content_info['tpl_id']));
            $this->assign('service_content_info', $service_content_info);
            $this->assign('service_id', $service_id);
            $this->display();  
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $ServiceContent = M('ServiceContent');
        if (false !== $ServiceContent->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    
	public function sortAction() {
        $service_id = I("get.service_id", 0, 'intval');
		$ServiceContent = D('ServiceContent');
        $arr_sort = I('post.sort');
		foreach ($arr_sort as $key => $val) {
			$data = array('sort' => intval($val));
			$ServiceContent->where('id = ' . $key)->save($data);
		}
		$this->redirect('ServiceContent/index', array('service_id' => $service_id));	
	}

    private function _getTpl($tpl_id)
    {
        return $this->fetch("Tpl/content/contentTpl" . $tpl_id);
    }
    
    public function ajaxGetTplAction()
    {
		$return = array('error_num' => 0, 'error_msg' => '', 'content' => '');
		$tpl_id = I('tpl_id', 0, 'intval');
        $content = $this->fetch("Tpl/content/contentTpl" . $tpl_id);
		$return['content'] = $content;
		$this->ajaxReturn($return);	
    }
    
	/**
     * 编辑器上传处理
     */
	public function uploadJsonAction() 
    {
		$config = array(
			'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'service/',
            'subPath' => 'service/',
		);
        vendor('Kindeditor.UploadJson', '', '.class.php');
		$UploadJson = new \UploadJson($config);
		$UploadJson->upload();
	}
    
	/**
     * 编辑器上传文件管理
     */
	public function fileManagerJson()
    {
		
	}
}